var APP_ID = 'wx878a12a5118620ac';
var HOST_URI = "https://card.adcomma.cn/";
var API_URI = HOST_URI + 'apixcx/';
var API_UPLOADS = HOST_URI + 'uploads/';

function Request(options) {
    var token = '';
    try {
        token = wx.getStorageSync("userInfo")['token'];
    } catch (e) {}
    wx.request({
        url: API_URI + options.url,
        data: options.data,
        method: options.method ? options.method : 'POST',
        header: {
            'App-Id': APP_ID,
            'Cache-Control': 'no-cache',
            'Content-Type': 'application/x-www-form-urlencoded',
            'Http-Token': token,
            'XX-Device-Type': 'comma',
        },
        success: res => {
            var data = res.data;
            options.success(data);
            return data;
        },
        fail: res => {
            if (options.fail) {
                options.fail(res)
            }
        },
        complete: options.complete ? options.complete : null
    });
}

function Post(options) {
    Request(options);
}

function Get(options) {
    options.method = 'GET';
    Request(options);
}

function getData(url, options, fn, cb) {
    Post({
        url: url,
        data: options,
        success: res => {
            fn(res)
            if (cb) {
                cb(res)
            }
            return res;
        },
        fail: res => {
            return 0;
        },
    });
}

function userLogin(data, cb) {
    getData('user/login', data, function (res) {
        try {
            wx.setStorageSync('userInfo', res);
        } catch (e) {}
    }, cb);
}

function getCardList(data, cb) {
    getData('Index/info', data, function (res) {
        return res;
    }, cb);
}

function getMember(data, cb) {
    getData('card/get', data, function (res) {
        return res;
    }, cb);
}

function saveUserInfo(data, cb) {
    getData('User/updataNamePhone', data, function (res) {
        return res;
    }, cb);
}

function cardGiveup(data, cb) {
    getData('Card/giveup', data, function (res) {
        return res;
    }, cb);
}

function upDateFormId(data, cb) {
    getData('user/updateformid', data, function (res) {
        return res;
    }, cb);
}

function RelationEdit(data, cb) {
    getData('Relation/edit', data, function (res) {
        return res;
    }, cb);
}


function saveMsg(data, cb) {
    getData('msg/save', data, function (res) {
        return res;
    }, cb);
}

function saveLog(data, cb) {
    getData('Log/saveLog', data, function (res) {
        return res;
    }, cb);
}

function checkLogin() {
    try {
        var userInfo = wx.getStorageSync("userInfo");
        if (userInfo) {
            return userInfo['id'];
        } else {
            return false;
        }
    } catch (e) {}
}

module.exports = {
    httpUrl: HOST_URI,
    API_UPLOADS: API_UPLOADS,
    userLogin: userLogin,
    checkLogin: checkLogin,
    getCardList: getCardList,
    getMember: getMember,
    saveLog: saveLog,
    saveMsg: saveMsg,
    saveUserInfo: saveUserInfo,
    RelationEdit: RelationEdit,
    cardGiveup: cardGiveup,
    upDateFormId: upDateFormId
};